﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using mapclient.Resources;
using Com.AMap.Api.Maps;
using Com.AMap.Api.Maps.Model;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media;
using Windows.Devices.Geolocation;
using System.Threading;
using Microsoft.WindowsAzure.MobileServices;
using mapclient.Resources;
using Newtonsoft.Json;


namespace mapclient
{
    public partial class MainPage : PhoneApplicationPage
    {
        System.Threading.Timer t;
        public static int a;
        AMap amap;
        LatLng s;
        AMapMarker marker;
        AMapGeolocator mylocation;
        private IMobileServiceTable<weizhi> ta = App.MobileService.GetTable<weizhi>();
        //圆
        ManualResetEvent ev = new ManualResetEvent(true);
        AMapCircle circle;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.ContentPanel.Children.Add(amap = new AMap());    
            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
            amap.Loaded += amap_Loaded;
            amap.Tap += amap_Tap;
        }

        void amap_Loaded(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();]
            amap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(39.90923, 116.397428), 13));
            mylocation = new AMapGeolocator();
          clb(null);
            //触发位置改变事件
           
       //   t = new System.Threading.Timer(clb);
         //  t.Change(0, 20000);
           // Thread.Sleep(10000);
        }

        private void clb(object state)
        {
            a = 1;
            mylocation.DesiredAccuracy = PositionAccuracy.High;
            mylocation.Start();//
            mylocation.PositionChanged += mylocation_PositionChanged;
           
        //   Thread.Sleep(15000);
          // mylocation.PositionChanged -= mylocation_PositionChanged;
         //  mylocation.Stop();
         //  throw new NotImplementedException();
        }

      async  void mylocation_PositionChanged(AMapGeolocator sender, AMapPositionChangedEventArgs args)
        {
          if (ev.WaitOne())
          {
              Thread.Sleep(900000);
            //  a--;
            if (!App.RunningInBackground)
            {
                this.Dispatcher.BeginInvoke(() =>
                {

                    //添加圆
                    circle = amap.AddCircle(new AMapCircleOptions()
                     {
                         Center = args.LngLat,//圆点位置
                         Radius = (float)args.Accuracy,//半径
                         FillColor = System.Windows.Media.Color.FromArgb(80, 100, 150, 255),
                         StrokeWidth = 2,//边框粗细
                         StrokeColor = Color.FromArgb(80, 0, 0, 255),//边框颜色

                     });
              
                    //添加点标注，用于标注地图上的点
                    marker = amap.AddMarker(
                   new AMapMarkerOptions()
                   {
                       Position = args.LngLat,//图标的位置
                       Title = "我的位置",
                       IconUri = new Uri("Images/marker_gps_no_sharing.png", UriKind.Relative),//图标的URL
                       Anchor = new Point(0.5, 0.5),//图标中心点

                   });
                    MessageBox.Show(args.LngLat.latitude.ToString() + args.LngLat.longitude.ToString());
                });
                weizhi w = new weizhi() { lx = args.LngLat.latitude.ToString(), ly = args.LngLat.longitude.ToString() };
                 await    ta.InsertAsync(w);
                
            }
            else 
            {
                weizhi w = new weizhi() { lx = args.LngLat.latitude.ToString(), ly = args.LngLat.longitude.ToString() };
                await ta.InsertAsync(w);
                Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
                toast.Content = args.LngLat.ToString();
                toast.Title = "ok: ";
                //  toast.NavigationUri = new Uri("/Page2.xaml", UriKind.Relative);
                toast.Show();
            }
           
            ev.Set();
        }
      }
        void amap_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //throw new NotImplementedException();
            Point p = e.GetPosition(amap);//获得在map容器的像素坐标

            s = amap.GetProjection().FromScreenLocation(p);
            marker = amap.AddMarker(new AMapMarkerOptions()
            {
                Position = s,
                Title = "Title",
                Snippet = "Snippet",
                IconUri = new Uri("Images/AZURE.png", UriKind.Relative),
            });
        }
       
        // 用于生成本地化 ApplicationBar 的示例代码
        //private void BuildLocalizedApplicationBar()
        //{
        //    // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
        //    ApplicationBar = new ApplicationBar();

        //    // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // 使用 AppResources 中的本地化字符串创建新菜单项。
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}